package cn.sise.edu.crm.web.controller;

import cn.sise.edu.crm.domain.JsonResult;
import cn.sise.edu.crm.domain.Permission;
import cn.sise.edu.crm.query.QueryObject;
import cn.sise.edu.crm.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by chen on 2019/11/5.
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/list")
    @RequiresPermissions(value = {"权限页面", "permission:list"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/permission/list";
    }
    @RequestMapping("/reload")
    @ResponseBody
    @RequiresPermissions(value = {"加载权限", "permission:reload"},logical = Logical.OR)
    public JsonResult reload(){
        try {
            permissionService.reload();
        }catch (Exception e){
            return new JsonResult("加载失败",false);
        }
        return new JsonResult("加载成功",true);
    }
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"权限删除", "permission:delete"},logical = Logical.OR)
    public JsonResult delete(Long id){
        try {
            permissionService.delete(id);
        }catch (Exception e){
            return new JsonResult("删除失败",false);
        }

        return new JsonResult("删除成功",true);
    }
}
